import { message } from 'antd';
import ROUTES from '@/routes/config';
import { MENUS } from '@/constants';
import { store } from '@/decorators';
import { route, menu } from '@/utils';
import api from '@/services';

const { merge } = store;

@merge({
  // 系统名称
  systemName: 'react-mobx-template',
  // 路由数组
  routeArr: [],
  // 路由映射
  routeMap: {},
  // 是否折叠侧边栏
  foldSider: false,
  // 权限映射
  authMap: null,
  // 菜单数组
  menuArr: []
}, {
  caller: api,
  items: [
    // 退出
    { key: 'logout' }
  ]
})
class Store {
  constructor() {
    this.init();
    const { routeArr, routeMap } = route.format(ROUTES);
    Object.assign(this, { routeArr, routeMap });
  }

  async queryMenus() {
    try {
      // const data = await api.queryMenus();
      const data = MENUS;
      const { menuArr, authMap } = menu.format(data);
      this.update({ menuArr, authMap });
    } catch (error) {
      message.error(error.message);
    }
  }
}

export default new Store();
